package com.example.auth.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Configuration
@EnableSwagger2 // 启用 Swagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        // 添加header参数
        List<Parameter> headers = Stream.of(
                new ParameterBuilder().name("authorization").description("access token").modelRef(new ModelRef("string")).parameterType("header").required(true).defaultValue("Bearer").build()).collect(Collectors.toList());

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 指定要扫描的Controller的包路径
                .apis(RequestHandlerSelectors.basePackage("com.example.auth.controller"))
                // 所有路径
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(headers);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("登录模块文档")
                .description("这是一个登录模块的API文档示例")
                .version("1.0")
                .build();
    }
}
